package com.liao.app2.ui.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.liao.app2.ui.bean.User;

import java.util.ArrayList;

public class UserDB extends SQLiteOpenHelper {

    public UserDB(@Nullable Context context, @Nullable String name, int version) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table user(userId Integer primary key autoincrement,username varchar(10),password varchar(10))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "ALTER TABLE user ADD COLUMN sex VARCHAR(20)";
        db.execSQL(sql);
        String sql1 = "create table user_bp(userId Integer primary key autoincrement,username varchar(10),password varchar(10))";
        db.execSQL(sql1);
        ArrayList<User> allUser = findAllUser1(this.getReadableDatabase());
        for (User user : allUser) {
            saveData1(this.getWritableDatabase(), user);
        }
    }

    public boolean saveData(SQLiteDatabase db, User user) {
        ContentValues values = new ContentValues();
        values.put("username", user.getName());
        values.put("password", user.getPw());
        if (user.getSex() != null) {
            values.put("sex", user.getSex());
        }
        long user1 = db.insert("user", null, values);
        db.close();
        if (user1 > 0) {
            return true;
        } else {
            return false;
        }
    }

    private void saveData1(SQLiteDatabase db, User user) {
        ContentValues values = new ContentValues();
        values.put("username", user.getName());
        values.put("password", user.getPw());
        if (user.getSex() != null) {
            values.put("sex", user.getSex());
        }
        long user1 = db.insert("user_bp", null, values);
        db.close();
    }

    public ArrayList<User> findAllUser(SQLiteDatabase db) {
        ArrayList<User> users = new ArrayList<>();
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            User user = new User();
            user.setName(cursor.getString(cursor.getColumnIndex("username")));
            user.setPw(cursor.getString(cursor.getColumnIndex("password")));
            users.add(user);
        }
        return users;
    }

    public ArrayList<User> findAllUser1(SQLiteDatabase db) {
        ArrayList<User> users = new ArrayList<>();
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            User user = new User();
            user.setName(cursor.getString(cursor.getColumnIndex("username")));
            user.setPw(cursor.getString(cursor.getColumnIndex("password")));
            user.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            users.add(user);
        }
        return users;
    }

    public boolean findUser(SQLiteDatabase db, String username, String password) {
        Cursor cursor = db.query("user", null, "username=? and password=?", new String[]{username, password}, null, null, null);
        if (cursor.getCount() > 0) {
            return true;
        }
        return false;
    }

}
